package safeexcise;

class Consumer extends Thread{
    Account account = new Account();

//    public Consumer(Account acct){
//        this.account = acct;
//    }

    public Consumer(Account acct,String name){
        super(name);
        this.account = acct;
    }
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for(int i=0;i<3;i++) {
            account.add(1000);
        }

    }


}
class Account{
    int money = 0;

    public synchronized void add(int amt){
        money += amt;
        System.out.println(Thread.currentThread().getName() + "存入1000元,"+"余额为："+money);
    }

}
public class AccountTest {
    public static void main(String[] args) {

        Account acct = new Account();

        Consumer c1 = new Consumer(acct,"甲");
        Consumer c2 = new Consumer(acct,"乙");

        c1.start();
        c2.start();

    }
}
